package com.tool_stdy.utils.guava;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.google.common.collect.Sets.SetView;

public class GuavaCollectionsUtils {

    /**
     * 只读
     */
    public void ListReadOnly() {
        List<String> gList = new ArrayList<String>();
        gList.add("a");
        gList.add("b");
        gList.add("C");
        ImmutableList<String> copyOf = ImmutableList.copyOf(gList);
        copyOf.add("d");
    }

    /**
     * 集合过滤器  只获取返回true的东西
     */
    public static void CollectionsFilter() {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add(null);
        System.out.println(list.size());
        Collection<String> result = Collections2.filter(list, new Predicate<String>() {
            public boolean apply(String src) {
                try {
                    Preconditions.checkNotNull(src);
                } catch (Exception e) {
                    return false;
                }
                return true;
            }
        });
        System.out.println(result.size());
        for (String src : result) {
            System.out.println(src);
        }
    }

    /**
     * 集合中每个元素都执行 function
     */
    public static void CollectionsFunctions() {
        Set<Integer> set = Sets.newHashSet();
        set.add(1);
        set.add(2);
        set.add(3);
        Collection<String> srcC = Collections2.transform(set, new Function<Integer, String>() {
            public String apply(Integer input) {
                return input + "_";
            }
        });
        for (String src : srcC) {
            System.out.println(src);
        }
    }

    /**
     * 多个function组合
     */
    public static void CollectionsFunctionsCom() {
        Set<String> set = Sets.newHashSet();
        set.add(1 + "");
        set.add(2 + "");
        set.add(3 + "");
        Function<String, String> f1 = new Function<String, String>() {
            public String apply(String input) {
                return input + "_";
            }
        };

        Function<String, String> f2 = new Function<String, String>() {
            public String apply(String input) {
                return input + "||";
            }
        };
        //先执行f1在执行f2
        Function<String, String> f = Functions.compose(f2, f1);
        Collection<String> srcC = Collections2.transform(set, f);
        for (String src : srcC) {
            System.out.println(src);
        }
    }

    /**
     * 集合的交集/差集/并集
     */
    public static void Collectionsjiaochaoji() {
        Set<Integer> set1 = Sets.newHashSet(1, 2, 4, 5);
        Set<Integer> set2 = Sets.newHashSet(2, 3, 4, 6, 7);
        //交集
        SetView<Integer> setView = Sets.intersection(set1, set2);
        for (Integer t : setView) {
            System.out.println(t);
        }
        //差集:根顺序有关系
        System.out.println("差集:");
        SetView<Integer> difference = Sets.difference(set2, set1);
        for (Integer dif : difference) {
            System.out.println(dif);
        }
        //并集
        System.out.println("并集:");
        SetView<Integer> union = Sets.union(set2, set1);
        for (Integer dif : union) {
            System.out.println(dif);
        }
    }

    public static void main(String[] args) {
        //CollectionsFilter();
        //CollectionsFilter();
        //CollectionsFunctions();
        //CollectionsFunctionsCom();
        Collectionsjiaochaoji();
    }
}
